// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;

namespace SnapObjects.Data
{
    internal abstract class SqlJoinOnBuilder : SqlJoinOnAndOr<ISqlJoinOnAndOr>, ISqlJoinOnAndOr, ISqlJoinOnBuilder
    {
        protected ISqlWhereBuilder _where;

        public SqlJoinOnBuilder(
            ISqlBuilder builder, ISqlWhereBuilder where, SqlBuildClause join) : base(builder, join)
        {
            _where = where;
        }

        protected override ISqlJoinOnAndOr BuilderResult()
        {
            return this;
        }

        private ISqlJoinOnAndOr AddOn(ConditionElement condition)
        {
            if (_current != null)
            {
                _current.On = condition;
            }
            else
            {
                throw new InvalidOperationException("Please call 'Join' before adding conditions.");
            }

            return this.BuilderResult();
        }

        /// <summary>
        ///     Creates an ON clause and adds a search condition to it. Specifies SQL expressions on both the left and
        ///     right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlJoinOnAndOr object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public ISqlJoinOnAndOr On(string left, string right)
        {
            return this.AddOn(
                new ConditionElement(left, SqlOperator.Equals, right, SqlRightType.Clause));
        }

        /// <summary>
        ///     Creates an ON clause and adds a search condition to it. Specifies SQL expressions on both the left and
        ///     right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns></returns>
        public ISqlJoinOnAndOr On(string left, SqlBinaryOperator sqlOperator, string right)
        {
            return this.AddOn(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), right, SqlRightType.Clause));
        }

        /// <summary>
        ///     Creates an ON clause and adds a search condition to it. Specifies a SQL expression on the left of the 
        ///     operator and a SQL parameter on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the ISqlJoinOnAndOr object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public ISqlJoinOnAndOr On(string left, ISqlParameter parameter)
        {
            _clause.AddParameter(parameter);

            return this.AddOn(
                new ConditionElement(left, SqlOperator.Equals, parameter, SqlRightType.Parameter));
        }

        /// <summary>
        ///     Creates an ON clause and adds a search condition to it. Specifies a SQL expression on the left of the 
        ///     operator and a SQL parameter on the right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the ISqlJoinOnAndOr object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public ISqlJoinOnAndOr On(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter)
        {
            _clause.AddParameter(parameter);

            return this.AddOn(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), parameter, SqlRightType.Parameter));
        }

        /// <summary>
        ///     Creates an ON clause and adds a search condition to it. Specifies the SQL expression on the left of the operator and
        ///     the SQL subquery on the right. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the ISqlJoinOnAndOr object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public ISqlJoinOnAndOr On(string left, ISqlBuilder sqlBuilder)
        {
            var builder = (SqlBuilderBase)sqlBuilder;

            return this.AddOn(
                new ConditionElement(left, SqlOperator.Equals, builder.Statement, SqlRightType.Statement));
        }

        /// <summary>
        ///     Creates an ON clause and adds a search condition to it. Specifies a SQL expression on the left of the operator 
        ///     and a SQL subquery on the right.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the ISqlJoinOnAndOr object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public ISqlJoinOnAndOr On(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder)
        {
            var builder = (SqlBuilderBase)sqlBuilder;

            return this.AddOn(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), builder.Statement, SqlRightType.Statement));
        }

        /// <summary>
        ///     Creates an ON clause and adds a search condition to it using an ISqlJoinCondition object.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlJoinCondition which represents a search condition in the ON Clause.
        ///     This object can be created by using the SqlBuilder.On method and so on.
        /// </param>
        /// <returns>
        ///     Returns the ISqlJoinOnAndOr object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public ISqlJoinOnAndOr On(ISqlJoinCondition condition)
        {
            var hc = (SqlJoinCondition)condition;

            if (hc.Clause.TryGet(out WhereElement cjoin))
            {
                _clause.AddParameters(hc.BuilderParameters);

                return this.AddOn(cjoin.Condition);
            }
            else
            {
                throw new ArgumentException("Invalid 'On' condition.");
            }
        }

        /// <summary>
        ///     Creates an ON clause and adds a raw SQL to it.
        /// </summary>
        /// <param name="clause">A string of raw SQL to be added to the ON clause.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the ISqlJoinOnAndOr object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public ISqlJoinOnAndOr OnRaw(string clause, params ISqlParameter[] parameters)
        {
            var condition = new ConditionElement(null, SqlOperator.Equals, clause, SqlRightType.Clause);

            _clause.AddParameters(parameters);

            return this.AddOn(condition);
        }

        /// <summary>
        ///     Creates an ON clause and adds a search condition to it. Specifies the SQL expression on the left of the 
        ///     operator and a specific value on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="value">The specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlJoinOnAndOr object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public ISqlJoinOnAndOr OnValue(string left, object value)
        {
            return this.AddOn(
                new ConditionElement(left, SqlOperator.Equals, value, SqlRightType.Value));
        }

        /// <summary>
        ///     Creates an ON clause and adds a search condition to it. Specifies a SQL expression on the left of the 
        ///     operator and a specific value on the right of the operator.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the expressions on the left and the value on the right.
        /// </param>
        /// <param name="value">The specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlJoinOnAndOr object which can be used to add more search conditions to the current ON clause.
        /// </returns>
        public ISqlJoinOnAndOr OnValue(string left, SqlBinaryOperator sqlOperator, object value)
        {
            return this.AddOn(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), value, SqlRightType.Value));
        }

        #region Where
        /// <summary>
        ///     Determines whether any search condition has been added to the WHERE clause of the current 
        ///     object using a Where or WhereRaw method and so on.
        /// </summary>
        public bool HasWhere => _where.HasWhere;

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. Specifies a SQL 
        ///     expression on the left of the operator and a specific value on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="value">A specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr WhereValue(string left, object value)
        {
            return _where.WhereValue(left, value);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. Specifies a SQL expression on 
        ///     the left of the operator and a specific value on the right of the operator.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the expression on the left and the value on the right.
        /// </param>
        /// <param name="value">A specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr WhereValue(string left, SqlBinaryOperator sqlOperator, object value)
        {
            return _where.WhereValue(left, sqlOperator, value);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. Specifies a SQL expression 
        ///     on the left of the operator and a SQL parameter on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr Where(string left, ISqlParameter parameter)
        {
            return _where.Where(left, parameter);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. Specifies a SQL expression 
        ///     on the left of the operator and a SQL parameter on the right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr Where(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter)
        {
            return _where.Where(left, sqlOperator, parameter);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. 
        ///     Specifies SQL expressions on both the left and right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr Where(string left, string right)
        {
            return _where.Where(left, right);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. 
        ///     Specifies SQL expressions on both the left and right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr Where(string left, SqlBinaryOperator sqlOperator, string right)
        {
            return _where.Where(left, sqlOperator, right);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. Specifies a SQL expression on the left 
        ///     of the operator and a SQL subquery on the right. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlBuilder">An ISqlBuilder object which represents a SQL subquery on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr Where(string left, ISqlBuilder sqlBuilder)
        {
            return _where.Where(left, sqlBuilder);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. Specifies a SQL expression on
        ///     the left of the operator and a SQL subquery on the right.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="sqlBuilder">An ISqlBuilder object which represents a SQL subquery on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr Where(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder)
        {
            return _where.Where(left, sqlOperator, sqlBuilder);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause by an ISqlWhereCondition object.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlWhereCondition which represents a search condition in the WHERE clause. 
        ///     This object can be created by using the SqlBuilder.Where method and so on.
        /// </param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr Where(ISqlWhereCondition condition)
        {
            return _where.Where(condition);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a raw SQL to the WHERE clause.
        /// </summary>
        /// <param name="whereClause">A string of raw SQL to be added to the WHERE clause.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which defines the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr WhereRaw(string whereClause, params ISqlParameter[] parameters)
        {
            return _where.WhereRaw(whereClause, parameters);
        }

        /// <summary>
        ///     Creates a WHERE clause, adds a search condition to the WHERE clause by an ISqlWhereCondition object, 
        ///     and specifies a name for the search condition.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlWhereCondition which represents a search condition in the WHERE clause. 
        ///     This object can be created by using the SqlBuilder.Where method and so on.
        /// </param>
        /// <param name="whereName">
        ///     The name for the search condition. It can be used to remove a search condition from the WHERE clause.
        /// </param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr Where(ISqlWhereCondition condition, string whereName)
        {
            return _where.Where(condition, whereName);
        }

        /// <summary>
        ///     Creates a WHERE clause, adds a raw SQL to the WHERE clause, and specifies a name for the raw SQL.
        /// </summary>
        /// <param name="whereClause">A string of raw SQL to be added to the WHERE clause.</param>
        /// <param name="whereName">The name for the raw SQL. It can be used to remove the raw SQL from the WHERE clause.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which defines the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr WhereRaw(string whereClause, string whereName, params ISqlParameter[] parameters)
        {
            return _where.WhereRaw(whereClause, whereName, parameters);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. 
        ///     Uses IS NULL operator to search for values that are null.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the IS NULL operator.</param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr WhereIsNull(string left)
        {
            return _where.WhereIsNull(left);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. 
        ///     Uses IS NOT NULL operator to search for values that are not null.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the IS NOT NULL operator.</param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr WhereIsNotNull(string left)
        {
            return _where.WhereIsNotNull(left);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. Uses EXISTS to test for the 
        ///     existence of the record returned from a subquery.
        /// </summary>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the EXISTS operator.
        /// </param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr WhereExists(ISqlBuilder sqlBuilder)
        {
            return _where.WhereExists(sqlBuilder);
        }

        /// <summary>
        ///     Creates a WHERE clause, and adds a search condition to the WHERE clause. Uses the NOT EXISTS 
        ///     operator to test if the record returned from a subquery does not exist.
        /// </summary>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the EXISTS operator.
        /// </param>
        /// <returns>
        ///     Returns the ISqlWhereAndOr object which can be used to add more search conditions to the current WHERE clause.
        /// </returns>
        public ISqlWhereAndOr WhereNotExists(ISqlBuilder sqlBuilder)
        {
            return _where.WhereNotExists(sqlBuilder);
        }
        #endregion

        /// <summary>
        ///     Removes the search condition (specified by name) from the WHERE clause.
        /// </summary>
        /// <param name="whereName">The name for one or more search conditions in the WHERE clause.</param>
        /// <returns>
        ///     Returns true if the removal is successful, and returns false if the specified search
        ///     condition name does not exist in the WHERE clause.
        /// </returns>
        public bool RemoveWhere(string whereName)
        {
            return _where.RemoveWhere(whereName);
        }

        /// <summary>
        ///     Removes all of the search conditions that have been added to the WHERE clause.
        /// </summary>
        /// <returns>
        ///     Returns true if the removal is successful, and returns false if there were no search conditions in the WHERE clause.
        /// </returns>
        public bool RemoveWhere()
        {
            return _where.RemoveWhere();
        }
        #region GroupBy
        /// <summary>
        ///     Creates a GROUP BY clause and adds an array of SQL expressions (each expression specifies
        ///     a column or a non-aggregate calculation on a column).
        /// </summary>
        /// <param name="groupExpressions">
        ///     An array of SQL expressions (each expression specifies a column or a non-aggregate calculation on a column).
        /// </param>
        /// <returns>
        ///     Returns the current ISqlGroupBuilder object.
        /// </returns>
        public ISqlGroupBuilder GroupBy(params string[] groupExpressions)
        {
            return _where.GroupBy(groupExpressions);
        }

        /// <summary>
        ///     Creates a GROUP BY clause, and adds a raw SQL string containing one or more column expressions (each expression
        ///     specifies a column or a non-aggregate calculation on a column).
        /// </summary>
        /// <param name="groupClause">
        ///     A raw SQL string containing one or more column expressions (each expression specifies a column or a 
        ///     non-aggregate calculation on a column).
        /// </param>
        /// <returns>
        ///     Returns the current ISqlGroupBuilder object.
        /// </returns>
        public ISqlGroupBuilder GroupBy(string groupClause)
        {
            return _where.GroupBy(groupClause);
        }

        /// <summary>
        ///     Returns an ISqlGroupBuilder object which represents an object used to build the GROUP BY clause in a SQL statement.
        /// </summary>
        /// <returns>
        ///     Returns the ISqlGroupBuilder object which represents an object used to build the GROUP BY clause in a SQL statement.
        /// </returns>
        public ISqlGroupBuilder AsGroupBy()
        {
            return _where.AsGroupBy();
        }
        #endregion

        #region Having
        /// <summary>
        ///     Creates a HAVING clause, and adds a search condition to the HAVING clause. You can specify 
        ///     a SQL expression on the left the operator and a specific value on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="value">
        ///     The specific value on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr HavingValue(string left, object value)
        {
            return _where.HavingValue(left, value);
        }

        /// <summary>
        ///     Creates a HAVING clause, and adds a search condition to the HAVING clause. You can specify the SQL 
        ///     expression on the left the operator and a specific value on the right of the operator.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator which is the operator to test the expression on the left and the value on the right.
        /// </param>
        /// <param name="value">
        ///     The specific value on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr HavingValue(string left, SqlBinaryOperator sqlOperator, object value)
        {
            return _where.HavingValue(left, sqlOperator, value);
        }

        /// <summary>
        ///     Creates a HAVING clause, and adds a search condition to the HAVING clause. You can specify a SQL 
        ///     expression on the left of the operator and a SQL parameter on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator. 
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr Having(string left, ISqlParameter parameter)
        {
            return _where.Having(left, parameter);
        }

        /// <summary>
        ///     Creates a HAVING clause and adds a search condition to the HAVING clause. You can specify a SQL expression 
        ///     on the left of the operator and a SQL parameter on the right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator. 
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr Having(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter)
        {
            return _where.Having(left, sqlOperator, parameter);
        }

        /// <summary>
        ///     Creates a HAVING clause and adds a search condition to the HAVING clause. You can specify SQL expressions
        ///     on both the left and right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr Having(string left, string right)
        {
            return _where.Having(left, right);
        }

        /// <summary>
        ///     Creates a HAVING clause and adds a search condition to the HAVING clause. You can specify SQL expressions 
        ///     on both the left and right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr Having(string left, SqlBinaryOperator sqlOperator, string right)
        {
            return _where.Having(left, sqlOperator, right);
        }

        /// <summary>
        ///     Creates a HAVING clause and adds a search condition to the HAVING clause. You can specify a SQL expression on the 
        ///     left of the operator and a SQL subquery on the right side. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlBuilder">An ISqlBuilder object which represents a SQL subquery on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr Having(string left, ISqlBuilder sqlBuilder)
        {
            return _where.Having(left, sqlBuilder);
        }

        /// <summary>
        ///     Creates a HAVING clause and adds a search condition to the HAVING clause. You can specify a SQL expression on the 
        ///     left of the operator and a SQL subquery on the right side.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="sqlBuilder">An ISqlBuilder object which represents a SQL subquery on the right of the operator.</param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr Having(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder)
        {
            return _where.Having(left, sqlOperator, sqlBuilder);
        }

        /// <summary>
        ///     Creates a HAVING clause, and adds a search condition to the HAVING clause by an ISqlHavingCondition object.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlHavingCondition object which represents a search condition in the HAVING clause.
        ///     This object can be created by using the SqlBuilder.Having method and so on.
        /// </param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr Having(ISqlHavingCondition condition)
        {
            return _where.Having(condition);
        }

        /// <summary>
        ///     Creates a HAVING clause and adds a raw SQL to the HAVING clause.
        /// </summary>
        /// <param name="havingClause">A string of raw SQL to be added to the HAVING clause.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr HavingRaw(string havingClause, params ISqlParameter[] parameters)
        {
            return _where.HavingRaw(havingClause, parameters);
        }

        /// <summary>
        ///     Creates a HAVING clause, and adds a search condition to the HAVING clause by an ISqlHavingCondition 
        ///     object and specifies the name for the search condition.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlHavingCondition object which represents a search condition in the HAVING clause.
        ///     This object can be created by using the SqlBuilder.Having method and so on.
        /// </param>
        /// <param name="havingName">The name for the search condition.</param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr Having(ISqlHavingCondition condition, string havingName)
        {
            return _where.Having(condition, havingName);
        }

        /// <summary>
        ///     Creates a HAVING clause, and adds a raw SQL to the HAVING clause and specifies a name for the raw SQL.
        /// </summary>
        /// <param name="havingClause">A string of raw SQL to be added to the HAVING clause.</param>
        /// <param name="havingName">The name for the raw SQL.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public ISqlHavingAndOr HavingRaw(string havingClause, string havingName, params ISqlParameter[] parameters)
        {
            return _where.HavingRaw(havingClause, havingName, parameters);
        }

        /// <summary>
        ///     Returns an ISqlHavingAndOr object that can be used to add a search condition to a group or an aggregate in the HAVING clause.
        /// </summary>
        /// <returns>
        ///     Returns the ISqlHavingAndOr object that can add a search condition to a group or an aggregate in the HAVING clause.
        /// </returns>
        public ISqlHavingAndOr AsHaving()
        {
            return _where.AsHaving();
        }
        #endregion

        #region OrderBy
        /// <summary>
        ///     Creates an ORDER BY clause and specifies one or more sort criteria in ascending order.
        /// </summary>
        /// <param name="orderExpressions">
        ///     (Optional) An array of expressions which specify a column or an expression on which to sort the query result set.
        /// </param>
        /// <returns>
        ///     Returns an ISqlOrderThenBuilder object which can be used to add more sort criteria to the current ORDER BY clause.
        /// </returns>
        public ISqlOrderThenBuilder OrderByAscending(params string[] orderExpressions)
        {
            return _where.OrderByAscending(orderExpressions);
        }

        /// <summary>
        ///     Creates an ORDER BY clause and specifies one or more sort criteria in descending order.
        /// </summary>
        /// <param name="orderExpressions">
        ///     (Optional) An array of expressions which specify a column or an expression on which to sort the query result set.
        /// </param>
        /// <returns>
        ///     Returns an ISqlOrderThenBuilder object which can be used to add more sort criteria to the current ORDER BY clause.
        /// </returns>
        public ISqlOrderThenBuilder OrderByDescending(params string[] orderExpressions)
        {
            return _where.OrderByDescending(orderExpressions);
        }

        /// <summary>
        ///     Creates an ORDER BY clause and specifies one or more sort criteria using a raw SQL.
        /// </summary>
        /// <param name="orderClause">A string of raw SQL which specifies one or more sort criteria.</param>
        /// <returns>
        ///     Returns an ISqlOrderThenBuilder object which can be used to add more sort criteria to the current ORDER BY clause.
        /// </returns>
        public ISqlOrderThenBuilder OrderByRaw(string orderClause)
        {
            return _where.OrderByRaw(orderClause);
        }

        /// <summary>
        ///     Creates an ORDER BY clause; and specifies one or more sort criteria using a raw SQL and specifies an alias for the raw SQL.
        /// </summary>
        /// <param name="orderClause">A string of raw SQL which specifies one or more sort criteria.</param>
        /// <param name="alias">The alias for the raw SQL.</param>
        /// <returns>
        ///     Returns an ISqlOrderThenBuilder object which can be used to add more sort criteria to the current ORDER BY clause.
        /// </returns>
        public ISqlOrderThenBuilder OrderByRaw(string orderClause, string alias)
        {
            return _where.OrderByRaw(orderClause, alias);
        }

        /// <summary>
        ///     Returns an ISqlOrderThenBuilder object which can be used to add sorting criteria to the ORDER clause.
        /// </summary>
        /// <returns>
        ///     Returns an ISqlOrderThenBuilder object which can be used to add sorting criteria to the ORDER clause.
        /// </returns>
        public ISqlOrderThenBuilder AsOrderBy()
        {
            return _where.AsOrderBy();
        }
        #endregion
    }
}
